﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DjBlog.Admin
{
    public partial class Post : System.Web.UI.Page
    {
        protected DjModel.PostInfo mpost = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            object o = HttpContext.Current.Session["loginuser"];
            if (o==null||o.ToString() != "djqq")
            {
                Response.Redirect("/admin/login.aspx");
                return;
            }
            if (!IsPostBack)
            {
                int id = DjCommon.Utils.GetQueryInt("postid",0);
                if (id > 0)
                {
                    mpost = DjBll.Post.GetPostInfo(id, DjModel.PostGetEnum.Delete);
                }
                if (mpost == null)
                {
                    mpost = new DjModel.PostInfo();
                }
            }
            else
            {
                SavePost();
            }
        }

        private void SavePost()
        {
            mpost = new DjModel.PostInfo();
            mpost.ID = DjCommon.Utils.GetFormInt("postid");
            if (mpost.ID > 0)
            {
                mpost = DjBll.Post.GetPostInfo(mpost.ID, DjModel.PostGetEnum.Delete);
            }

            mpost.Author = "djqq";
            if (Request.Form.GetValues("cat") != null)
            {
                mpost.CatString = string.Join(",", Request.Form.GetValues("cat"));
            }
            else
            {
                mpost.CatString = string.Empty;
            }
            mpost.CommentCount = 0;
            mpost.Content = DjCommon.Utils.GetFormString("content");
            mpost.CreateDate = DateTime.Now;
            mpost.Excerpt = DjCommon.Utils.GetFormString("des");
            mpost.PostStatus = 0;
            if (Request.Form.GetValues("tag") != null)
            {
                mpost.TagString = string.Join(",", Request.Form.GetValues("tag"));
            }
            else
            {
                mpost.TagString = string.Empty;
            }
            mpost.Title = DjCommon.Utils.GetFormString("title");

            int res = -1;
            if (mpost.ID == 0)
            {
                res = DjBll.Post.AddPost(mpost);
            }
            else
            {
                res = DjBll.Post.UpdatePost(mpost);
            }
        }
    }
}
